import { createI18n } from 'vue-i18n'
import enUS from './en-US'
import zhCN from './zh-CN'
import { useAppStore } from '../store/modules/app'
import { getActivePinia } from 'pinia'

const defaultLocale = getActivePinia() ? useAppStore().getLanguage : 'zh-CN'

const i18n = createI18n({
  locale: defaultLocale,
  // locale:'en-US',
  fallbackLocale: 'en-US',
  allowComposition: true,
  messages: {
    'en-US': enUS,
    'zh-CN': zhCN
  }
})

export const t = i18n.global.t
export type Language = 'en-US' | 'zh-CN'
export function setLocale(locale: Language) {
  i18n.global.locale = locale
}

export function setupI18n(app: any) {
  const appStore = useAppStore() // **这里才调用 store，避免初始化问题**
  //@ts-ignore
  i18n.global.locale = appStore.getLanguage 
  app.use(i18n)
}

export default i18n
